package nl.hu.verbrandendephoenix.controller;

import nl.hu.verbrandendephoenix.client.AppManager;
import nl.hu.verbrandendephoenix.client.NotificationManager;
import nl.hu.verbrandendephoenix.shared.FieldVerifier;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Panel;

public class AddAuction {
	private nl.hu.verbrandendephoenix.view.AddAuction addAuctionView;
	private int error;
	final FieldVerifier fv = FieldVerifier.get();
	final NotificationManager nM = NotificationManager.get();
	
	public AddAuction() {
		

		/*AppManager.get().getRpcService().getUserData(Cookies.getCookie("loginString"), new AsyncCallback<User>() {
			public void onFailure(Throwable caught) {
				nM.commError();
			}
			
			public void onSuccess(User result) {
				if(result != null) { */
				/*} 
				
				else {
					nM.SID_Invalid();
				}
			}
			
		}); */
	}
	
	public Panel getPanel() {
		//System.out.println("getPanel called");
		addAuctionView = new nl.hu.verbrandendephoenix.view.AddAuction();
		//System.out.println("addAuctionView created");
		Panel p = addAuctionView.getAddAuctionForm();
		//System.out.println("Getting panel");
		addHandlers();
		return p;
	}
	
	public void addHandlers() {
		Button btnConfirm = addAuctionView.getButtonConfirm();
		btnConfirm.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				confirmAddAuction();
			} 
		});
	}

	private void confirmAddAuction() {
		NotificationManager nM =  NotificationManager.get();
		if(!checkFieldsSuccesful()) {
			nM.AuctionCheck(error);
		} else {
			nM.createNotification("Submit is aangeroepen");
			
			AppManager.get().getRpcService().createAuction(addAuctionView.getAuctName().getText(), addAuctionView.getDescription().getText(), Integer.parseInt(addAuctionView.getIniPrice().getText()), Cookies.getCookie("loginString"), addAuctionView.getCategory().getItemText(addAuctionView.getCategory().getSelectedIndex()), new AsyncCallback<Boolean>() {
				NotificationManager nM =  NotificationManager.get();
				public void onSuccess(Boolean result) {
					if (result) {
						nM.createNotification("De veiling is aangemaakt");
						History.newItem("myAuctions");
					}
					else {
						Window.alert("Something has gone wrong.");
					}
				}
				public void onFailure(Throwable caught) {
					nM.commError();
				}
			});
		}
	}
	
	private boolean checkFieldsSuccesful() {
		boolean checkers = true;
		/*
		 * Auction naam:
		 * - min 5 char
		 * - max 32 char
		 */
		if(addAuctionView.getAuctName().getText().length() < 5 || addAuctionView.getAuctName().getText().length() > 32) {
			if(addAuctionView.getAuctName().getText().length() < 5) {
				error = 1;
			}
			
			else {
				error = 2;
			}			
			fv.addRequired(addAuctionView.getAuctName());
			checkers = false;
		}
		
		else {
			fv.removeRequired(addAuctionView.getAuctName());
		}
		
		/*
		 * categorie: niet null.
		 */
		if(addAuctionView.getCategory().getSelectedIndex() == 0) {
			checkers = false;
			error = 3;
			fv.addRequired(addAuctionView.getCategory());
		}
		
		else {
			fv.removeRequired(addAuctionView.getCategory());
		}
		
		if(addAuctionView.getDescription().getText().length() < 10 || addAuctionView.getDescription().getText().length() > 1000) {
			if(addAuctionView.getDescription().getText().length() < 10) {
				error = 4;
			}
			
			else {
				error = 5;
			}
			checkers = false;
			fv.addRequired(addAuctionView.getDescription());
		}
		
		else {
			fv.removeRequired(addAuctionView.getDescription());
		}
		
		if(addAuctionView.getIniPrice().getText().length() == 0) {
			error = 7;
			checkers = false;
			fv.addRequired(addAuctionView.getIniPrice());
		}
		
		else{
			fv.removeRequired(addAuctionView.getIniPrice());
		}
		
		
		try {
			Integer.parseInt(addAuctionView.getIniPrice().getText());
			fv.removeRequired(addAuctionView.getIniPrice());
		}
		
		catch(NumberFormatException nfe) {
			error = 6;
			checkers = false;
			fv.addRequired(addAuctionView.getIniPrice());
		}
		return checkers;
	}
}
